package cn.jorian.framework.net;

import cn.jorian.framework.net.converter.GsonConverterFactory2;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import java.util.concurrent.TimeUnit;

/**
 * retrofit单例
 */
public class RetrofitUtil {

    //连接超时
    private static final int timeout = 1 * 60;
    private final Retrofit retrofit;
    private static RetrofitUtil retrofitUtil;
//    private Map<HttpUrl, List<Cookie>> cookieMap;

    private RetrofitUtil(String baseUrl) {
//        cookieMap = new HashMap<>();
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(timeout, TimeUnit.SECONDS)
                .readTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS);
//                .cookieJar(new CookieJar() {
//                    @Override
//                    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
//                        cookieMap.put(httpUrl, list);
//                    }
//
//                    @Override
//                    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
//                        List<Cookie> cookies = cookieMap.get(httpUrl);
//                        if (cookies == null) {
//                            cookies = new ArrayList<>();
//                        }
//                        return cookies;
//                    }
//                });

//        builder.addNetworkInterceptor(chain -> {
//            Request original = chain.request();
//            System.out.println(original.toString());
//            System.out.println(chain.proceed(original).body().string());
//            return chain.proceed(original);
//        });
//        //拦截器
//        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
//            @Override
//            public void log(String s) {
//                //重定向日志输出
//                log.error(s);
//            }
//        });
//        //设置拦截器日志级别
//        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        //设置拦截器
//        builder.addInterceptor(loggingInterceptor);
        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    public static RetrofitUtil newInstance(String baseUrl) {
        if (retrofitUtil == null) {
            synchronized (RetrofitUtil.class) {
                if (retrofitUtil == null) {
                    retrofitUtil = new RetrofitUtil(baseUrl);
                }
            }
        }
        return retrofitUtil;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }

//    public Map<HttpUrl, List<Cookie>> getCookieMap() {
//        return cookieMap;
//    }


}
